﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using U9API.Pub;
using System.Data;
using U9API.JsonModel;
using Newtonsoft.Json;

namespace U9API.APIHelper
{

    public class CurrentStockHelper : BaseHelper<JsonModel.ApiQuery>
    { 
        public List<U9API.DBHelper.Model.Customer> Resdatas { get; set; }
        public string result;

        protected override void Create(string U9constr, int operation, List<JsonModel.ApiQuery> models )
        {
            Pub.WriteLog.WriteLine(DateTime.Now.ToShortDateString() + ": CustomerHelper");
            if (models.Count == 0)
            {
                throw new Exception("查询条件不允许为空!");
            }
            JsonModel.ApiQuery model = models[0];
            StringBuilder sql = new StringBuilder();
            sql.Append("select a.ItemInfo_ItemID, a.ItemInfo_ItemCode, a.ItemInfo_ItemName, a.StoreMainUOM, a.StoreQty, a.Wh, c.code as whCode, b.name as whname ");
            sql.Append(" from InvTrans_WhQoh a with(nolock) LEFT JOIN cbo_Wh c with(nolock) on c.ID = a.Wh LEFT JOIN cbo_WH_Trl b with(nolock) on b.ID = a.Wh where 1=1 ");
            if (!string.IsNullOrEmpty(model.wh))
            {
                sql.Append(" and c.Code =" + model.wh);
            }
            if (!string.IsNullOrEmpty(model.id))
            {
                sql.Append(" and a.ItemInfo_ItemID =" + model.id);
            }
            if (!string.IsNullOrEmpty(model.code))
            {
                sql.Append("and a.ItemInfo_ItemCode = '" + model.code + "'");
            }
            if (!string.IsNullOrEmpty(model.startdate))
            {
                sql.Append(" and (a.CreatedOn >='" + model.startdate + "' or a.ModifiedOn >='" + model.startdate +"')");
            }
            if (!string.IsNullOrEmpty(model.enddate))
            {
                sql.Append(" and (a.CreatedOn <='" + model.enddate + "' or a.ModifiedOn <='" + model.enddate + "')");
            }
            DataSet ds = DbHelperSQL.Query(sql.ToString(), U9constr);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                //List<U9API.DBHelper.Model.Customer> datas = new List<DBHelper.Model.Customer>();
                //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                //{
                //    datas.Add(U9API.DBHelper.DAL.Customer.DataRowToModel(ds.Tables[0].Rows[i]));                    
                //}
                //Resdatas = datas;
                result = JsonConvert.SerializeObject(ds.Tables[0]);
            }
            else
            {
                throw new Exception("没有查到匹配的现存量记录!");
                
            }
        }
         
    }
}
